package pl.polidea.imf.blackberry.util;

import java.util.Vector;

import microlog.net.sf.microlog.core.Logger;
import microlog.net.sf.microlog.core.LoggerFactory;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

public class Settings implements Persistable {
	
	static Logger logger = LoggerFactory.getLogger(Settings.class);
	
	public static final int SETTINGS_VERSION = 0;
	private static final long PERSISTENT_STORE_SETTINGS_ID = 0xc5527edb10d343a2L;
	
	static final int NUMBER_OF_FIELDS = 3;
	private Vector fields;
	
	private static final int KEY_SETTINGS_VERSION = 0;
	
	public static final int KEY_REG_KEY = 1;
	public static final int KEY_IS_CRASH = 2;
	
	
	private static Settings settings;
	
	private Settings() {
		resetSettings();
	}
	
	
	public static synchronized Settings getInstance() {
		if (settings == null) {
			PersistentObject store = PersistentStore.getPersistentObject(PERSISTENT_STORE_SETTINGS_ID);
			settings = (Settings) store.getContents();

			if (settings == null || settings.getFieldInt(Settings.SETTINGS_VERSION) != Settings.SETTINGS_VERSION) {
				
				settings = new Settings();
				store.setContents(settings);
				PersistentObject.commit(store);

			}
		}

		return settings;
	}
	
	public void commit() {
		PersistentObject store = PersistentStore.getPersistentObject(PERSISTENT_STORE_SETTINGS_ID);
		synchronized (store) {
			store.setContents(settings);
			PersistentObject.commit(store);
		}
	}

	public static void clear() {
		PersistentObject store = PersistentStore.getPersistentObject(PERSISTENT_STORE_SETTINGS_ID);
		synchronized (store) {
			store.setContents(null);
			PersistentObject.commit(store);
		}
	}

	private synchronized void resetSettings() {

		fields = new Vector(NUMBER_OF_FIELDS);

		for (int i = 0; i < NUMBER_OF_FIELDS; i++) {
			fields.addElement(null);
		}
		
		fields.setElementAt(new Integer(SETTINGS_VERSION), KEY_SETTINGS_VERSION);
	}

	public synchronized Object getField(int id) {
		Object result = null;
		try {
			result = fields.elementAt(id);

		} catch (Exception e) {
			logger.debug("Cannot get object by id: "+id, e);
		}
		return result;

	}

	public synchronized int getFieldInt(int id) {
		int result = 0;
		try {
			result = ((Integer) fields.elementAt(id)).intValue();
		} catch (Exception e) {
			logger.debug("Cannot get int by id: "+id, e);
		}
		return result;
	}

	public synchronized long getFieldLong(int id) {
		long result = 0;
		try {
			result = ((Long) fields.elementAt(id)).longValue();
		} catch (Exception e) {
			logger.debug("Cannot get long by id: "+id, e);
		}
		return result;
	}

	public synchronized boolean getFieldBool(int id) {
		boolean result = false;
		try {
			result = ((Boolean) fields.elementAt(id)).booleanValue();
		} catch (Exception e) {
			logger.debug("Cannot get bool by id: "+id, e);
		}
		return result;
	}

	public synchronized void setField(int id, Object value) {
		if(id>=NUMBER_OF_FIELDS || id<0){
			throw new IllegalArgumentException("Cannot set field uner id: "+id);
		}
		if (id < fields.size())
			fields.setElementAt(value, id);
		else {
			fields.setSize(id + 1);
			fields.setElementAt(value, id);
		}
	}
}
